package rpg.visual.menu;

import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics2D;
import java.awt.event.MouseEvent;

import pgui.PVisual;
import pgui.visual.PComponent;
import pgui.visual.PImage;
import rpg.visual.VisualImage;

public abstract class VisualMenuItem extends PComponent {
	private String title;
	private PImage image;
	protected boolean hover = false, clicked = false;

	public VisualMenuItem(PVisual visual, String title, PImage image) {
		super(visual);
		this.setTitle(title);
		
		this.setWidth(40);
		this.setHeight(40);

		this.setImage(image);
	}

	// Mouse Events
	@Override
	public void onMouseMoved(MouseEvent event, MouseEvent lastEvent) {
		super.onMouseMoved(event, lastEvent);
		hover = true;
		setCursor(Cursor.HAND_CURSOR);
	}
	
	@Override
	public void onMouseDragged(MouseEvent event, MouseEvent lastEvent) {
		super.onMouseDragged(event, lastEvent);
		hover = true;
		clicked = true;
	}
	
	@Override
	public void onMousePressed(MouseEvent event) {
		super.onMousePressed(event);
		clicked = true;
	}
	
	@Override
	public void onMouseReleased(MouseEvent event) {
		super.onMouseReleased(event);
		clicked = false;
	}
	
	@Override
	public void onMouseExited(MouseEvent event, MouseEvent lastEvent) {
		super.onMouseExited(event, lastEvent);
		hover = false;
		clicked = false;
		setCursor(Cursor.DEFAULT_CURSOR);
	}

	// Paint Events
	@Override
	public void onPaint(Graphics2D g) {
		super.onPaint(g);

		if (hover) {			
			if(clicked)
				g.setColor(Color.darkGray);
			else g.setColor(new Color(120, 120, 120));
			
			VisualImage.opacity(g, 0.25f);
			g.fillRoundRect(0, 0, this.getLastWidth(), this.getLastHeight(), 0, 0);
		}
		
		VisualImage.opacity(g, 0.9f);
		g.translate(2, 2);
		image.draw(g);
		
		g.translate(0, -2);
		g.setColor(Color.darkGray);
		VisualImage.opacity(g, 0.25f);
		g.drawRect(this.getLastWidth(), 0, 0, this.getLastHeight());
	}

	// Get & Set
	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public PImage getImage() {
		return image;
	}

	public void setImage(PImage image) {
		this.image = image;
	}
}
